<template>
  <div id="orgSettings">
    <div v-for="session in sessions" >
      {{session.name}}
    </div>
    <button id="deleteOrg" class="btn btn-sm btn-danger" v-on:click="deleteOrg()">
      <i class="fa fa-trash"></i>
      Delete Organization
    </button>
  </div>
</template>

<script>
  import {ipcRenderer} from 'electron';
  import fs from 'fs-extra';
  import path from 'path';

  const {app} = require('electron').remote;
  export default {
    name: 'orgSettings',
    data: function () {
      return {
        sessions: [],
        cloudTypes: ['awsRegion', 'googleProject'],
        cloudServicesExpanded: {},
        cloudConfig: {},
        cloudKeys: {},
        cloudUsers: {},
        cloudSettings: {},
        cloudInstances: [],
        org: {},
        cloudServices: [],
        awsRegionCount: 0,
        googleProjectCount: 0,
        newCloudService: {},
        newCloudServiceIdentifier: {},
        scanning: {},
        customUser: {},
        useBastion: {},
        isWindows: (process.platform === 'win32') ? 1 : 0,
        tab: null,
        awsUsers: [
          'ec2-user',
          'ubuntu',
          'centos',
          'root'
        ],
        regionSSHUsers: [],
        projectSSHUsers: []
      };
    },
    mounted: function () {
      ipcRenderer.on('orgsettings.loadData', async (e, data) => {
        this.getOrgData(data);
        this.tab = data.tab;
        if (data.cloudSettings) {
          this.editCloud(data.cloudSettings, 'settings');
        }
      });
    },
    methods: {
      /**
       * Delete organization
       */
      deleteOrg: async function () {
        if (confirm('Are you sure you want to delete this Organization?  All associated configs and SSH keys will be purged.')) {
          this.$db.cloudServices.remove({org: this.org._id}, {multi: true});
          this.$db.orgs.remove({_id: this.org._id});
          const appData = app.getPath('home');
          const dirPath = path.join(appData, '.opshell', this.org.name).replace(/\s+/g, '-');
          fs.emptyDir(dirPath);
          ipcRenderer.send('orgmenu.updateSelectBox');
          ipcRenderer.send('orgmenu.updateSelectedOrg');
          ipcRenderer.send('mainview.closeTab', this.tab);
          ipcRenderer.send('alertbox.show', {type: 'tempMessage', msg: this.org.name + ' has been deleted'});
        } else {
          return false;
        }
      }
    }
  };
</script>
<style>
  @import '../../assets/css/orgsettings.css';
</style>
